package com.kma.summer2012.dao;

import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.model.CrumbsPosition;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:52
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsPositionDAOImpl implements CrumbsPositionDAO {
    @Override

    public void add(CrumbsPosition position) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(position);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsPosition position) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(position);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsPosition getById(int position_id) {
        Session session = null;
        CrumbsPosition position = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            position = (CrumbsPosition) session.get(CrumbsPosition.class, position_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return position;
    }

    @Override
    public Collection<CrumbsPosition> getAll() {
        Session session = null;
        List positions = new ArrayList<CrumbsPosition>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            positions = session.createCriteria(CrumbsPosition.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return positions;
    }

    @Override
    public void delete(CrumbsPosition position) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(position);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

}
